package Note.util;
import java.security.*;
public class MD5Tool
{
	//该方法将你输入的字符串，通过md5加密，返回一个加密后的字符串
	public static String MD5Encrypt(String strIn)
	{
		MessageDigest md = null;
		String strOut = null;
		try
		{
			md = MessageDigest.getInstance("MD5"); //可以选中其他的算法如SHA
			byte[] digest = md.digest(strIn.getBytes());
			//返回的是byet[]，要转化为String存储比较方便
			strOut = bytetoString(digest);
		}
		catch (NoSuchAlgorithmException nsae)
		{
			nsae.printStackTrace();
		}
		return strOut;
	}
	public static String bytetoString(byte[] digest)
	{
		String str = "";
		String tempStr = "";
		for (int i = 1; i < digest.length; i++)
		{
			tempStr = (Integer.toHexString(digest[i] & 0xff));
			if (tempStr.length() == 1)
			{
				str = str + "0" + tempStr;
			}
			else
			{
				str = str + tempStr;
			}
		}
		return str.toLowerCase();
	}
	public static void main(String []args)
	{
		String test="liuzc";
		String result=MD5Tool.MD5Encrypt(test);
		System.out.println (test+" 加密后的结果是:"+result);
	}
}

